package com.pearadmin.shop.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.shop.domain.ShopGoods;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.shop.service.IShopGoodsService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 店铺商品Controller
 *
 * @author jmys
 * @date 2021-05-06
 */
@RestController
@RequestMapping("/shop/goods")
public class ShopGoodsController extends BaseController
{
    private String prefix = "shop/goods";

    @Autowired
    private IShopGoodsService shopGoodsService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/shop/goods/main','shop:goods:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询店铺商品列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/shop/goods/data','shop:goods:data')")
    public ResultTable list(@ModelAttribute ShopGoods shopGoods, PageDomain pageDomain)
    {
        PageInfo<ShopGoods> pageInfo = shopGoodsService.selectShopGoodsPage(shopGoods,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 查询店铺商品列表
     */
    @ResponseBody
    @GetMapping("/data/all")
    @PreAuthorize("hasPermission('/shop/goods/data','shop:goods:data')")
    public Result listAll(@ModelAttribute ShopGoods shopGoods, PageDomain pageDomain)
    {
        List<ShopGoods> list = shopGoodsService.selectShopGoodsAll();
        return Result.success(list);
    }

    /**
     * 新增店铺商品
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/shop/goods/add','shop:goods:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存店铺商品
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/shop/goods/add','shop:goods:add')")
    public Result save(@RequestBody ShopGoods shopGoods)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        shopGoods.setCreateTime(LocalDateTime.now());
        shopGoods.setCreateBy(sysUser.getUserId());
        shopGoods.setCreateName(sysUser.getUsername());
        return decide(shopGoodsService.insertShopGoods(shopGoods));
    }

    /**
     * 修改店铺商品
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/shop/goods/edit','shop:goods:edit')")
    public ModelAndView edit(Long id, ModelMap mmap)
    {
        ShopGoods shopGoods = shopGoodsService.selectShopGoodsById(id);
        mmap.put("shopGoods", shopGoods);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存店铺商品
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/shop/goods/edit','shop:goods:edit')")
    public Result update(@RequestBody ShopGoods shopGoods)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        shopGoods.setUpdateTime(LocalDateTime.now());
        shopGoods.setUpdateBy(sysUser.getUserId());
        shopGoods.setUpdateName(sysUser.getUsername());
        return decide(shopGoodsService.updateShopGoods(shopGoods));
    }

    /**
     * 删除店铺商品
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/shop/goods/remove','shop:goods:remove')")
    public Result batchRemove(String ids)
    {
        return decide(shopGoodsService.deleteShopGoodsByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/shop/goods/remove','shop:goods:remove')")
    public Result remove(@PathVariable("id") Long id)
    {
        return decide(shopGoodsService.deleteShopGoodsById(id));
    }
}
